# -*- coding: utf-8 -*-

import datetime

from mdcssport.mdcsapp.models import PointsLog

def get_user_points_history(game_type_id, user):
    """
    Za zadatog korisnika vraca njegove poene kao niz tuple-ova -> [(date, chess_points), ...]
    """
    history = []
    points_logs = PointsLog.objects.filter(match__game_type=game_type_id).filter(user = user).order_by('match__date_played')
    last_change_date = None
    today = datetime.datetime.today().date()
    i = 0
    history.append((user.user.date_joined.date(), 1200))
    while True:
        if i >= len(points_logs):
            break
        point_log_date = points_logs[i].match.date_played.date()
        while (i+1 < len(points_logs)) and (point_log_date == points_logs[i+1].match.date_played.date()):
            i = i + 1
        last_change_date = point_log_date
        if (last_change_date != today) and (i+1 < len(points_logs)):
            history.append((last_change_date, points_logs[i+1].points))
        i = i + 1

    if last_change_date != None:
        history.append((last_change_date, user.points(game_type_id)))
    if last_change_date != today:
        history.append((today, user.points(game_type_id)))
    return history